package com.fruit.service.order.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fruit.mapper.order.OrderMapper;
import com.fruit.pojo.common.Page;
import com.fruit.pojo.order.Address;
import com.fruit.pojo.order.ComAccount;
import com.fruit.pojo.order.Invoice;
import com.fruit.pojo.order.Order;
import com.fruit.pojo.order.StockRecordCourier;
import com.fruit.service.order.OrderManager;
import com.fruit.util.PayConstant;
import com.fruit.util.ResultInfo;
import com.fruit.util.ResultUtil;

@Service
public class OrderService implements OrderManager {

	@Autowired
	private OrderMapper orderMapper;

	
	@Override
	public List<Order> getOrderList(Page page) {
		return orderMapper.getOrderList(page);
	}
	
	@Override
	public List<Order> getOrderDetails(String oid) {
		Map<String, Object> resMa = new HashMap<String, Object>();
		List<Map<String, Object>> resMaps=orderMapper.getOrderDetails(oid);
		if(resMaps==null || resMaps.size()<0){
			resMa.put("result", ResultUtil.initResult(ResultInfo.ERROR, "params_error", "未找到订单!"));
		}
		Address shop=orderMapper.getOrderReceiver(oid);
		if(shop == null){
			resMa.put("result", ResultUtil.initResult(ResultInfo.ERROR, "address_error", "未找到订单收货地址!"));
		}
		ComAccount ca=orderMapper.getComAss();
		if(ca == null){
			resMa.put("result", ResultUtil.initResult(ResultInfo.ERROR, "account_error", "未找到订单收款账户!"));
		}
		Invoice i=orderMapper.getInvoice(oid);
		if(i != null){
			if("00".equals(i.getType())){
				i.setType("个人");
			}else{
				i.setType("公司");
			} 
		}
		Map<String, Order> dateilMap = new HashMap<String, Order>();
		for (Map<String, Object> resMap : resMaps) {
			if("00".equals(resMap.get("state"))){
				resMap.put("stateName", "未付款");
			}else if("02".equals(resMap.get("state"))){
				resMap.put("stateName", "待发货");
			}else if("03".equals(resMap.get("state"))){
				resMap.put("stateName", "待收货");
			}else if("04".equals(resMap.get("state"))){
				resMap.put("stateName", "已完成");
			}else if("05".equals(resMap.get("state"))){
				resMap.put("stateName", "已取消");
			}else if("06".equals(resMap.get("state"))){
				resMap.put("stateName", "已退款");
			}
			if(PayConstant.PAY_TYPE_ALL.equals(resMap.get("payType"))){
				resMap.put("payType", "全款");
			}
			Order g =new Order();
			g.setGoods_id((int)resMap.get("goodsId"));
			g.setGoodsThumb((String)resMap.get("goodsThumb"));
			g.setTitle((String)resMap.get("title"));
			g.setNumber((int)resMap.get("number"));
			g.setTradePrice((BigDecimal)resMap.get("tradePrice"));
			g.setTotalPrice((BigDecimal)resMap.get("totalPrice"));
			g.setRefundState((String)resMap.get("refundState1"));
			long id=(long)resMap.get("orderId");
			if (!dateilMap.containsKey(id + "s")) {
				Order o = new Order();
				o.getGoodsList().add(g);
				o.setPayMode((String)resMap.get("payMode"));
				o.setPayType((String)resMap.get("payType"));
				o.setId((int)id);
				String codePre = (String) resMap.get("codePre");
				String codeSuf = (String) resMap.get("codeSuf");
				o.setCodePre(codePre);
				o.setCodeSuf(codeSuf);
				o.setOrderCode(codePre+id+codeSuf);
				o.setSupplierName((String) resMap.get("supplier"));
				o.setUserId((int)resMap.get("userId"));
				o.setState((String)resMap.get("state"));
				o.setTotalPrice((BigDecimal)resMap.get("totalPrice"));
				o.setStateName((String)resMap.get("stateName"));
				o.setRealPrice((BigDecimal)resMap.get("realPrice"));
				o.setCreateTime((String)resMap.get("createTime"));
				o.setClosingTime((String)resMap.get("closingTime"));
				o.setDeliveryTime((String)resMap.get("deliveryTime"));
				o.setExpectDeliveryTime((String)resMap.get("expectDeliveryTime"));
				o.setRemittanceCode((String)resMap.get("remittanceCode"));
				o.setRefundState((String)resMap.get("refundState"));
				o.setAccount(ca);
				o.setAddress(shop);
				o.setInvoice(i);
				if(i != null){
					if("00".equals(i.getType())){
						o.getInvoice().setType("00");
					}else{
						o.getInvoice().setType("01");
					}
				}
				dateilMap.put(Long.toString(id)+"s", o);
			}else{
				Order o = dateilMap.get(Long.toString(id)+"s");
				o.getGoodsList().add(g);
			}
		}
		Collection<Order> valueCollection = dateilMap.values();
		List<Order> resOrderList = new ArrayList<Order>(valueCollection); 
		return resOrderList;
	}
	/** 修改订单状态
	 * 修改时间：2016.07.21
	 */
	@Override
	public void updateOrderState(Order order) {
		orderMapper.updateOrderState(order);
	}
	/**
	 * 获取出入库记录信息id
	 * @param map
	 * @return
	 */
	@Override
	public int getStockRecord(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return orderMapper.getStockRecord(map);
	}
	/**
	 * 添加司机信息
	 * @param sc
	 */
	@Override
	public void saveRecordCourier(StockRecordCourier sc) {
		// TODO Auto-generated method stub
		orderMapper.saveRecordCourier(sc);
	}

	/**
	 * 获取订单的版本号
	 */
	@Override
	public Map<String, Object> getVersion(String oid) {
		// TODO Auto-generated method stub
		return orderMapper.getVersion(oid);
	}

	/**
	 * 	订单搜索
	 */
	@Override
	public List<Order> search(Page page) {
		return orderMapper.search(page);
	}
}
